﻿using ARPGDemo.Skill;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Tedu.ARPGDemo.Character
{
	/// <summary>
    /// 角色输入控制器：获取用户输入
    /// </summary>
    [RequireComponent(typeof(CharacterMotor))]
    //建立一个依赖，当把脚本拖到物体时同时拖动CharacterMotor到物体,多个用逗号隔开[RequireComponent(typeof(CharacterMotor),typeof(PlayerStatus))]
    public class CharacterInputController : MonoBehaviour {
        private ETCJoystick joystick;//摇杆
        private CharacterMotor charMotor;//马达
        private ETCButton[] skillButtons;
        private Animator anim;
        private PlayerStatus chStatus;
        private CharacterSkillSystem skillSystem;
        [Tooltip("最小攻击间隔")]
        public float minAttackInterval = 1.5f;
        [Tooltip("最大连击时间")]
        public float maxBatterTime = 3;
        //查找组件
        private void Awake()
        {
            joystick = FindObjectOfType<ETCJoystick>();
            charMotor = GetComponent<CharacterMotor>();
            skillButtons = FindObjectsOfType<ETCButton>();
            anim = GetComponentInChildren<Animator>();
            chStatus = GetComponent<PlayerStatus>();
            skillSystem = GetComponent<CharacterSkillSystem>();
        }
        //注册时间
        private void OnEnable()
        {
            //public delegate void UnityAction<T0>(T0 arg0);
            joystick.onMove.AddListener(OnJoustickMove);
            joystick.onMoveStart.AddListener(OnJoustickMoveStart);//播放跑步动画
            joystick.onMoveEnd.AddListener(OnJoustickMoveEnd);
            foreach (ETCButton etcBut in skillButtons)
            {
                if (etcBut.name == "BaseSkill")
                {
                    etcBut.onPressed.AddListener(OnButtonPressed);
                }
                else
                {
                    etcBut.onDown.AddListener(OnButtonDown);
                }
            }
        }

        //注销事件
        private void OnDisable()
        {
            //print("OnDisable");
            joystick.onMove.RemoveListener(OnJoustickMove);
            joystick.onMoveStart.RemoveListener(OnJoustickMoveStart);//注销跑步动画
            joystick.onMoveEnd.RemoveListener(OnJoustickMoveEnd);
            foreach (ETCButton etcBut in skillButtons)
            {
                if (!etcBut)
                {
                    continue;//如果按钮被销毁则跳过
                }
                if (etcBut.name == "BaseSkill")
                {
                    etcBut.onPressed.RemoveListener(OnButtonPressed);
                }
                else
                {
                    etcBut.onDown.RemoveListener(OnButtonDown);
                }
            }
        }
        //多个按钮 绑定同一方法
        //原生的EasyTouch不带参数，可以修改为带参数。
        //修改方法：onDown(F12)--OnDownHandler: UnityEvent<string>{}加个string--查看onDown所有引用：改变两处onDown.Invoke(name)增加名字name
        private void OnButtonDown(string buttonName)
        {
            if (chStatus.IsAttacking())
            {
                return;//ARPG类游戏当玩家正在攻击时，不执行另一个动作；也可在CharacterSkillSystem中建一个动作队列，注册取消方法解决
            }
            //****Bug跑的时候不能攻击，改为攻击优先，采用以上解决方法****
            if (chStatus.IsRuning())//自加
            {
                print("如果正在跑，按下按钮，则停止跑");
                OnJoustickMoveEnd();
            }
            //print("战斗吧");
            //print(buttonName);
            int skillId = 0;
            switch (buttonName)
            {
                //case "BaseSkill":skillId = 1001;break;
                case "Skill1": skillId = 1002; break;
                case "Skill2": skillId = 1003; break;
                default:
                    break;
            }
            skillSystem.AttackUseSkill(skillId, false);
            //测试
            //var data = GetComponent<CharacterSkillManager>().PrepareSkill(skillId);
            //if (data != null)
            //{
            //    GetComponent<CharacterSkillManager>().GenerateSkill(data);
            //}

        }
        private float lastPressedTime;
        private void OnButtonPressed()
        {
            if (chStatus.IsAttacking())
            {
                return;
            }
            //if (chStatus.IsRuning())//自加
            //{
            //    OnJoustickMoveEnd();
            //}
            //计算间隔 = 当前时间 - 最后按下时间
            var interval = Time.unscaledTime - lastPressedTime;
            //如果间隔过小 则退出
            if (interval < minAttackInterval) return;
            //间隔 < 最大连击时间 可以连击  否则不可以连击
            bool isBatter = interval < maxBatterTime;
            //是否可以连击
            skillSystem.AttackUseSkill(1001, isBatter);
            //记录最后按下时间
            lastPressedTime = Time.unscaledTime;//unscaledTime不受时间缩放影响的时间，实际时间，不受慢动作、暂停影响
        }

        private void OnJoustickMove(Vector2 dir)
        {
            if (chStatus.IsAttacking())//自加，当攻击时，移动无效
            {
                OnJoustickMoveEnd();
                return;
            }
            else if(!chStatus.IsRuning())//当没有攻击，也没有跑，执行跑步动画
            {
                //OnJoustickMoveEnd();
                OnJoustickMoveStart();
            }
            //参数dir二维：x 无 y
            //马达dir三维：x  0 z
            //print("dir=" + dir);
            Vector3 targetDir = new Vector3(dir.x, 0, dir.y);
            //print("vec3" + targetDir);
            //移动，调用马达移动方法
            charMotor.Movement(targetDir);
        }
        private void OnJoustickMoveStart()
        {
            anim.SetBool(chStatus.animParams.Run, true);
        }
        private void OnJoustickMoveEnd()
        {
            anim.SetBool(chStatus.animParams.Run, false);
        }

        
    }
}

